package com.r_z.service.impl;

import com.r_z.entity.Outbound;
import com.r_z.entity.OutboundDetails;
import com.r_z.entity.Repertory;
import com.r_z.mapper.OutboundMapper;
import com.r_z.mapper.RepertoryMapper;
import com.r_z.service.OutboundService;
import com.r_z.util.NumberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出库操作的impl实现层
 * @author 任俊杰
 * @version 1.0
 * @date 2023/5/29
 */
@Service
public class OutboundImpl implements OutboundService {
    //导入OutboundMapper接口
    @Resource
    private OutboundMapper outboundMapper;

    //库存 mapper类
    @Resource
    private RepertoryMapper repertoryMapper;

    /**
     * 查询出库信息
     * @param page 页码
     * @param limit 行数
     * @param wheres 条件
     * @return
     */
    @Override
    public List<Outbound> selectOutbound(Integer page, Integer limit, String wheres) {
        //计算页码
        page=(page-1)*limit;
        String w = where(wheres);
        System.out.println(w+"---------------");
        return outboundMapper.selectOutbound(page,limit,where(wheres));
    }

    /**
     * 拼接条件
     * @param nowTime 日期
     * @return 条件
     */

    public String where(String nowTime) {
        StringBuilder where = new StringBuilder();
        if (nowTime!=null && !nowTime.equals("")){
            where.append(" and o.nowTime='").append(nowTime).append("'");
        }
        return where.toString();
    }

    /**
     * 查询条数
     * @param nowTime 时间
     * @return 行数
     */
    @Override
    public int getTotal(String nowTime) {
        return outboundMapper.getTotal(where(nowTime));
    }

    /**
     * 查询出库详情
     * @param salesOrderId 销售id
     * @return 详情数据
     */
    @Override
    public OutboundDetails selectDetails(String salesOrderId) {
        return outboundMapper.selectOutboundDetails(salesOrderId);
    }


    /**
     * 出库审核
     * @param salesOrderId 销售订单的id
     * @return 返回一个标识 i>=1则成功 反之失败
     */
    @Override
    public int auditOutbound(String salesOrderId) {
        OutboundDetails outboundDetails = outboundMapper.selectOutboundDetails(salesOrderId);
        Repertory repertory = outboundMapper.selectRepertoryCount(outboundDetails.getProductId());
        System.out.println(outboundDetails.toString()+"-------0------");
        System.out.println(repertory.toString());
        int count=0;
        if(repertory!=null&&repertory.getQuantity()>=outboundDetails.getQuantity()){
            System.out.println("---aaaaaaaaaaaaaaaaaaaaaaaaaaaaa----");
            count=1;
            outboundMapper.updateOutboundCount(outboundDetails.getQuantity(),outboundDetails.getProductId());

            outboundMapper.updateOutbound(salesOrderId);

            outboundMapper.updateOutbound2(salesOrderId);
        }
        return count;
    }

}
